﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KaiXin.BLL;
using KaiXin.BLL.Interface;
using KaiXin.BLL.Tycoon;

namespace Windows
{
    public partial class MainWin : Form
    {

        #region field
        private ITycoon _myTycoon;
        private IKXTycoon _kxTycoon = new KXTycoon();
        private IOrder _myOrder;
        #endregion

        public MainWin()
        {
            InitializeComponent();
        }

        private void btnViewOrders_Click(object sender, EventArgs e)
        {
            dgvGrid.DataSource = _myOrder.GetMyOrders();
        }

        private void btnCreateDefaultOrder_Click(object sender, EventArgs e)
        {
            if (cbbDefaultOrderCondition.SelectedItem.ToString()=="大于")
            {
                _myOrder.CreateDefaultOrders(g => g.MinPrice >= long.Parse(txtOrderGoodsPirce.Text));
            }
            else
            {
                _myOrder.CreateDefaultOrders(g => g.MinPrice <= long.Parse(txtOrderGoodsPirce.Text));
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (Common.Login(txtEmail.Text,txtPWD.Text))
            {
                gbTycoon.Enabled = true;
                btnLogin.Enabled = false;
                txtPWD.Enabled = false;
                txtEmail.Enabled = false;
                cbbDefaultOrderCondition.SelectedIndex = 0;
                cbbGoodsCondition.SelectedIndex = 0;

                _myTycoon = new Tycoon(txtEmail.Text, txtPWD.Text);
                _myOrder = new Order(txtEmail.Text, txtPWD.Text);
            }
            else
            {
                MessageBox.Show("用户登录失败");
            }
        }

        private void btnGetTop10_Click(object sender, EventArgs e)
        {
            if (cbbGoodsCondition.SelectedItem.ToString() == "大于")
            {
                dgvGrid.DataSource = _kxTycoon.GetTOP10(g => g.MinPrice >= long.Parse(txtGoodsPrice.Text));
            }
            else
            {
                dgvGrid.DataSource = _kxTycoon.GetTOP10(g => g.MinPrice <= long.Parse(txtGoodsPrice.Text));
            }
        }

     

        private void txtPWD_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(null, null);
            }
        }
    }
}
